//
//  MCgoodsListShowCell.swift
//  StoreManage
//
//  Created by MC on 2018/8/31.
//  Copyright © 2018年 MC. All rights reserved.
//

import UIKit

typealias MCgoodsListShowCellClosure = (Int) -> Void

class MCgoodsListShowCell: UITableViewCell {
   
    var closure : MCgoodsListShowCellClosure?

    
    var row = 0
    
    
    var model : MCGoodsListModel = MCGoodsListModel() {
    
        didSet {
            iconImageView.sd_setImage(with: URL.init(string: model.navigateImage), placeholderImage: #imageLiteral(resourceName: "Reuse_placeholder_1_1"))
    
            let price = model.lowestPrice
            
            if price >= model.suggestedPrice {
                moneyLabel.text = MCString_judgemantDecimalsIsZero(figure: price)
            } else {
                moneyLabel.text = MCString_judgemantDecimalsIsZero(figure: price) + "~" + MCString_judgemantDecimalsIsZero(figure: model.suggestedPrice)
            }

            commissionView.rightLabel.text = model.goodsCommissionRate.stringValue + "%"
            shareView.rightLabel.text = MCString_judgemantDecimalsIsZero(figure: model.goodsCommissionMoney.intValue)
    
            self.lackLabel.isHidden = !model.isStockWarn.boolValue
    
    
            if model.selfSupport == 1 {
                nameLabel.attributedText = model.goodsName.MCAttributedString_textAttachment(text: model.goodsName, image: #imageLiteral(resourceName: "GoodsManage_ziying").MCImageWithTintMainColor(), imageFrame: CGRect(x: 0, y: -4, width: 60*0.6, height: 28*0.6))
            } else {
                nameLabel.text = model.goodsName
            }
        }
    }
    
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        backgroundColor = UIColor.MCGray_light
        
        self.addSubview(bgView)
        bgView.snp.remakeConstraints { (make) ->Void in
            make.edges.equalTo(self).inset(UIEdgeInsetsMake(0, 0, 5, 0))
        }
        
        bgView.addSubview(iconImageView)
        iconImageView.snp.remakeConstraints { (make) ->Void in
            make.left.top.equalTo(bgView).offset(10)
            make.size.equalTo(CGSize.init(width: 70, height: 70))
        }
        
        iconImageView.addSubview(lackLabel)
        lackLabel.snp.remakeConstraints { (make) ->Void in
            make.edges.equalTo(iconImageView)
        }
        
        
        
        bgView.addSubview(moneyLabel)
        moneyLabel.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(iconImageView.snp.right).offset(5)
            make.bottom.equalTo(iconImageView.snp.bottom).offset(-5)
            make.height.equalTo(14)
        }
        
        
        self.addSubview(nameLabel)
        nameLabel.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(iconImageView.snp.right).offset(5)
            make.right.equalTo(bgView).offset(-15)
            make.bottom.lessThanOrEqualTo(moneyLabel.snp.top).offset(-5)
            make.top.equalTo(iconImageView)
        }
        
        bgView.addSubview(shareButton)
        shareButton.snp.remakeConstraints { (make) ->Void in
            make.right.equalTo(-15)
            make.centerY.equalTo(moneyLabel)
            make.size.equalTo(CGSize.init(width: 60, height: 22))
        }
        
        bgView.addSubview(commissionView)
        commissionView.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(iconImageView)
            make.right.equalTo(nameLabel)
            make.top.equalTo(iconImageView.snp.bottom).offset(10)
            make.height.equalTo(15)
        }
        
        bgView.addSubview(shareView)
        shareView.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(iconImageView)
            make.right.equalTo(nameLabel)
            make.top.equalTo(commissionView.snp.bottom).offset(5)
            make.height.equalTo(15)
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    @objc func shareButtonClicked() {
        closure?(row)
    }
    
    
    lazy var bgView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.white
        return view
    }()
    
    lazy var iconImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.image = #imageLiteral(resourceName: "Reuse_placeholder_1_1")
        imageView.layer.borderColor = UIColor.MCGray_light.cgColor
        imageView.layer.borderWidth = 1
        imageView.layer.cornerRadius = 5
        imageView.layer.masksToBounds = true
        return imageView
    }()
    
    lazy var lackLabel: UILabel = {
        let label = UILabel()
        label.backgroundColor = UIColor.init(white: 0, alpha: 0.7)
        label.font = MCFont(16)
        label.textColor = UIColor.white
        label.textAlignment = NSTextAlignment.center
        label.layer.cornerRadius = 5
        label.layer.masksToBounds = true
        label.text = "缺货"
        return label
    }()
    
    public lazy var nameLabel: UILabel = {
        let label = UILabel.init()
        label.font = MCFont(14)
        label.textColor = UIColor.MCBlack
        label.numberOfLines = 0
        label.text = "XXX"
        return label
    }()
    
    
    lazy var shareButton: UIButton = {
        let button = UIButton.init(type: UIButtonType.custom)
        button.backgroundColor = UIColor.white
        button.titleLabel?.font = MCFont(13)
        button.setTitle("分享", for: UIControlState.normal)
        button.setTitleColor(UIColor.MCMain, for: UIControlState.normal)
        button.layer.cornerRadius = 11
        button.layer.masksToBounds = true
        button.layer.borderColor = UIColor.MCMain.cgColor
        button.layer.borderWidth = 1
        
        button.addTarget(self, action: #selector(shareButtonClicked), for: UIControlEvents.touchUpInside)
        return button
    }()
    
    public lazy var moneyLabel: UILabel = {
        let label = UILabel.init()
        label.font = MCFont(15)
        label.textColor = UIColor.MCMain
        label.text = "¥0"
        return label
    }()
    
    
    lazy var commissionView: MCShowBarView = {
        let view = MCShowBarView()
        view.leftLabel.text = "佣金比例"
        view.rightLabel.text = "X%"
        view.leftLabel.font = MCFont(12)
        view.rightLabel.font = MCFont(12)
        view.lineView.isHidden = true
        return view
    }()
    
    
    lazy var shareView: MCShowBarView = {
        let view = MCShowBarView()
        view.leftLabel.text = "分享最低获得"
        view.rightLabel.text = "X"
        view.leftLabel.font = MCFont(12)
        view.rightLabel.font = MCFont(12)
        view.lineView.isHidden = true
        return view
    }()

}
